<?php
//if not a real page (nobody called us here, just user trying to hack
if ((!isset($_SESSION['realpage']))
 || ($_SESSION['realpage'] == false))
 {
 	require './hack.php';
 	die(" ");
 }

 
error_reporting(E_ALL);

require_once "./class.InfoSingleton.php";
require_once "./class.BaseDBUtils.php";

if (0 > version_compare(PHP_VERSION, '5')) {
	die('This file was generated for PHP 5');
}


class BaseDBUtils
{
	private $hasharray;// = array('arg','mgl','asd','scw','rsd','gfd','jjn','ews','lkj','qwe','zxc',',mng','tew','qfg');
	private $sqlLink = null;
	public $error = "";
	
	public static function getConn()
 	{
 		$infoObj = InfoSingleton::getInstance();
 		$newconn = new DBConnection();
 		$newconn->connectDB($infoObj->getDBURL() ,$infoObj->getDBPORT(), $infoObj->getDBUSER(),$infoObj->getDBPASS(),$infoObj->getDBSCHEME());
 		return  $newconn;
	  // end of function getConn
 	} 
	
 	protected function execute($sql)
 	{
 		$rez =  mysql_query($sql,$this->sqlLink) or $this->error.="<br> new error : " . date("D M j G:i:s T Y"). mysql_error($this->sqlLink). " num ". mysql_errno();
 		if (!$rez)
 	 	{
 	 		return false;
	 	}
	 	return true;
 	}
 	
	// return_format 1 = rows, 2 = array, 3 = first result
	protected  function qry($sql, $return_format = 0)
	{		
		$query = mysql_query($sql,$this->sqlLink)  or $this->error.="<br> new error : " . date("D M j G:i:s T Y"). mysql_error($this->sqlLink). " num ". mysql_errno();
		switch ($return_format)
		{
			case 1:
				$query = mysql_fetch_row($query);
				return $query;
				break;
			case 2:
				if($query == false)
					return false;
				$query = mysql_fetch_array($query);
				return $query;
				break;
			case 3:
				$query = mysql_fetch_row($query);
				$query = $query[0];
				return $query;
			default:
				return $query;
		}
		// end of function qry
	}
	public function paramHandler($param)
	{
		if(strlen($this->error) > 50000)
		{
			$this->error = "";
		}
	    $param = $this->removeSlashes($param);
		return mysql_real_escape_string($param);
	}
	
	private function removeSlashes($param)
	{
		$remove = array("\\");
		return str_replace($remove, '', $param);
	}
	
	public function retainLines($param)
	{
		$change = array("\n", "\r\n", "\r","<br>","<br/>");
		return str_replace($change, '[dropline]', $param);
		
	}
	private function hasher($param)
	{
		$counter = 0;
		$ind = 0;
		while ($ind < strlen($param)) 
		{
			$counter+=$param[$ind];
			$ind+=1;
		}
		
		return $this->hasharray[$counter % sizeof($this->hasharray)];
	}
	
	protected function salt($user,$pass)
	{
		return md5( $this->hasher($user) . $pass );
	}
	
	public function connectDB( $url, $port, $username,$password,$db)
	{
		$this->hasharray = array("arg","mgl","asd","scw","rsd","gfd","jjn","ews","lkj","qwe","zxc","mng","tew","qfg");
		
		$urlwithport = $url;
		if ($port != null && $port != '')
		{
			$urlwithport = $url . ':' . $port ;
		}
		
		//make sure to close connection before trying a new one
		if ($this->sqlLink != null)
		{
			mysql_close($this->sqlLink);
		}

		$this->error = "url: \'". $urlwithport ."\' user: \'" . $username ."\' password: \'" . $password ."\' db: \'".$db."\'";
		
		$this->sqlLink = mysql_connect($urlwithport,$username,$password) 
						or $this->error.="<br> new error : " . date("D M j G:i:s T Y"). mysql_error(). " num ". mysql_errno() and 
						die("<!DOCTYPE HTML><html> <head> </head> <body><script type=\"text/javascript\">window.location = \"./Install.php\";</script></body></html>");

		if (!$this->sqlLink)
		{
			$this->error.="<br> new error : " . date("D M j G:i:s T Y"). mysql_error(). " num ". mysql_errno();
			die('Could not connect: ' . mysql_error() ." ".$urlwithport ." ". $username.$password);
		}

		if($db == null || $db == '')
		{
			$db = 'defaultDb';
		}
		$this->error .= "<br>mysqlClientInfo". mysql_get_client_info();
		$this->error .= "<br>mysqlServerInfo". mysql_get_Server_info();
		
		//mysql_select_db($db,$this->sqlLink)  or $this->error.="<br> new error : " . date("D M j G:i:s T Y"). mysql_error($this->sqlLink). " num ". mysql_errno();;
		
		$sqlSelectDB= "use $db ;";
		mysql_query($sqlSelectDB,$this->sqlLink);
		// end of function connect
	} 
	
 	private function __clone() {}
 
	public function genRandomString() {
		$length = 10;
		$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
		$maxind = strlen($characters);
		$string = "";
		for ($p = 0; $p < $length; $p++) {
			$offst   = mt_rand(0, strlen($characters));
			$string .= $characters[$offst % $maxind];
		}
		return $string;
	}
	public function checkdate($datestr)
	{
		$tmp=explode("'", $datestr);
		if (sizeof($tmp) > 1)
			return date('Y/m/d', mktime(0, 0, 0, date("m"), date("d")+1, date("y")));
		return $this->paramHandler($datestr);
	}
	public function calcdate($datestr)
	{
		$argstr=explode('/', $datestr);
		if ((!is_numeric($argstr[0]))|| (!is_numeric($argstr[1]))|| (!is_numeric($argstr[2])))
		{
			return date('Y/m/d', mktime(0, 0, 0, date("m"), date("d")+1, date("y")));
		} 
		return date('Y/m/d',mktime(0,0,0,$argstr[1],$argstr[0],$argstr[2]));
	}
	
	public function isValidEmail($email)
	{
		return filter_var($email, FILTER_VALIDATE_EMAIL);
	}
}

?>